package com.mobileClientKernel.android.commonLayer;

import android.content.Context;
import android.text.TextUtils;

import com.mobileClientKernel.android.commonLayer.JJLog;


public class SecurityEncrypt {

    private static String TAG = "SecurityEncrypt";
    private static boolean isInit = false;
    private static String LogModuleName = "SecurityEncrypt";
    static {
        System.loadLibrary("Encrypt");
//        Log.d("SecurityEncrypt", "loadLibrary !");
    }

    public static synchronized String getKeyStr(String k){
        JJLog._d("SecurityEncrypt", "[getKeyStr] k = "+k);
        if(!isInit){
            JJLog._e("SecurityEncrypt", "[getKeyStr] isInit = false");
            return null;
        }
        try {
            return Base64Utils.encode(getKey(k.getBytes()));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public static synchronized String getEncryptStr(String data, String key){
        try {
            /*  JJLog._d(TAG, "[getEncryptStr] data = " + data);*/
            if(data == null|| key == null){
                return null;
            }
            if(!isInit){
                JJLog._e("SecurityEncrypt", "[getEncryptStr] isInit = false");
                return null;
            }
            String encrypt = Base64Utils.byte2hex(encrypt(data.getBytes(),key.getBytes()));
//            JJLog._d(TAG, "[getEncryptStr] encrypt = " + encrypt);
            return encrypt;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public static synchronized String getDecryptStr(String data, String key){
        try {
//            JJLog._d(TAG, "[getDecryptStr] data = " + data);
            if(data == null|| key == null){
                return null;
            }

            if(!isInit){
                JJLog._e("SecurityEncrypt", "[getEncryptStr] isInit = false");
                return null;
            }

            byte[] decrypt = decrypt(Base64Utils.hex2byte(data),key.getBytes());
            String decryptdata = new String(decrypt,"UTF-8");
//            JJLog._d(TAG, "[getDecryptStr] decrypt = " + decryptdata);
            return decryptdata;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public static String securityEncryptInitKey = null;
    public static synchronized String initSecurityEncrypt(Context app, String appkey){
        JJLog._d("SecurityEncrypt","[initSecurityEncrypt] appkey = "+appkey);
        if (!TextUtils.isEmpty(securityEncryptInitKey) && !"null".equals(securityEncryptInitKey)) {
            JJLog._i(LogModuleName, "have init, use value: " + securityEncryptInitKey);
            return securityEncryptInitKey;
        }

        JJLog._i(LogModuleName, "securityEncryptInitKey, appkey: " + appkey);

        securityEncryptInitKey = init(app,appkey);
        if (!TextUtils.isEmpty(securityEncryptInitKey) && !"null".equals(securityEncryptInitKey)) {
            isInit = true;
        }

        return securityEncryptInitKey;
    }

    public static native byte[] encrypt(byte[] data,byte[] key) ;

    public static native byte[] decrypt(byte[] data,byte[] key) ;

    public static native byte[] getKey(byte[] key) ;

    public static native byte[] getId(byte[] id) ;

    private static native String init(Context app, String appkey);
}
